<template>
  <div class="login-container">
    <div class="login-form">
      <div class="login-brand">
        <div class="logo-container">
          <h1 class="logo">Noody</h1>
          <img src="../assets/noody-logo.svg" alt="Noody Logo" class="logo-image" />
        </div>
      </div>
      <div class="login-header">
        <h1>登录</h1>
        <p>欢迎回来，请登录您的账户</p>
      </div>
      
      <form @submit.prevent="handleLogin">
        <div class="form-group">
          <label for="username">用户名</label>
          <input 
            type="text" 
            id="username" 
            v-model="username" 
            placeholder="请输入用户名"
            required
          />
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <input 
            type="password" 
            id="password" 
            v-model="password" 
            placeholder="请输入密码"
            required
          />
        </div>
        
        <div class="form-options">
          <label class="remember-me">
            <input type="checkbox" v-model="rememberMe">
            记住我
          </label>
          <a href="#" class="forgot-password">忘记密码？</a>
        </div>
        
        <button type="submit" class="login-button">登录</button>
        
        <div class="register-link">
          <p>还没有账户？<router-link to="/register">立即注册</router-link></p>
        </div>
      </form>
      
      <!-- 错误提示 -->
      <div v-if="error" class="error-message">
        {{ error }}
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../store/user'

const router = useRouter()
const userStore = useUserStore()

const username = ref('')
const password = ref('')
const rememberMe = ref(false)
const error = ref('')

// 在组件挂载时检查是否已经登录
onMounted(() => {
  if (userStore.isLoggedIn) {
    router.push('/')
  }
})

// 处理登录逻辑
const handleLogin = () => {
  const success = userStore.login(username.value, password.value)
  if (success) {
    router.push('/')
  } else {
    error.value = userStore.error
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  width: 100%;
  background: linear-gradient(135deg, #e8f5e9 0%, #4CAF50 100%);
  margin: 0;
  padding: 0;
}

.login-form {
  background: white;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 500px;
  min-width: 300px;
}

.login-brand {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
  padding-top: 20px;
}

.logo-container {
  display: flex;
  align-items: center;
}

.logo {
  color: #4CAF50;
  font-weight: 700;
  margin: 0;
  font-size: 28px;
}

.logo-image {
  margin-left: 10px;
  width: 35px;
  height: 35px;
  vertical-align: middle;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h1 {
  margin: 0;
  font-size: 28px;
  color: #333;
}

.login-header p {
  margin: 10px 0 0;
  color: #666;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #333;
}

.form-group input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 16px;
  transition: border-color 0.3s;
}

.form-group input:focus {
  outline: none;
  border-color: #667eea;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.remember-me {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.remember-me input {
  margin-right: 5px;
}

.forgot-password {
  color: #667eea;
  text-decoration: none;
  transition: color 0.3s;
}

.forgot-password:hover {
  color: #764ba2;
  text-decoration: underline;
}

.login-button {
  width: 100%;
  padding: 12px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 5px;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  transition: background-color 0.3s;
}

.login-button:hover {
  background-color: #45a049;
}

.register-link {
  text-align: center;
  margin-top: 20px;
}

.register-link a {
  color: #667eea;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s;
}

.register-link a:hover {
  color: #764ba2;
  text-decoration: underline;
}

.error-message {
  margin-top: 15px;
  padding: 10px;
  background: #f8d7da;
  color: #721c24;
  border: 1px solid #f5c6cb;
  border-radius: 5px;
  text-align: center;
}
</style>